package top.yaofengqiao.springcloudsimple.auth.biz.domain.auth.service;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import top.yaofengqiao.springcloudsimple.auth.biz.domain.auth.model.entity.UserOauth2Entity;

/**
 * @author yfq
 * @date 2024/6/11 14:57
 * @description
 */
@Slf4j
public abstract class AbstractOauth2Service implements IOauth2Service {

    @Override
    public UserOauth2Entity handleAuthenticationCode(String code) {
        log.info("Oauth2授权开始：code：{}", code);

        // 1. 获取accessToken
        String accessToken = getAccessToken(code);

        // 2. 使用accessToken换取用户信息
        UserOauth2Entity userOauth2Entity = getUserInfo(accessToken);

        log.info("Oauth2授权完成：code：{}，userOauth2Entity：{}", code, JSON.toJSONString(userOauth2Entity));
        return userOauth2Entity;
    }

    /**
     * 使用code换取accessToken
     *
     * @param code 授权code
     * @return accessToken
     */
    protected abstract String getAccessToken(String code);

    /**
     * 使用accessToken获取用户信息
     *
     * @param accessToken ...
     * @return oauth2返回的标准用户信息
     */
    protected abstract UserOauth2Entity getUserInfo(String accessToken);

}
